package com.shop.milk_tea_shop.controller;

import com.shop.milk_tea_shop.common.CodeEnum;
import com.shop.milk_tea_shop.common.GlobalInfo;
import com.shop.milk_tea_shop.common.Result;
import com.shop.milk_tea_shop.dto.ItemAddDTO;
import com.shop.milk_tea_shop.entity.ItemAdd;
import com.shop.milk_tea_shop.entity.ItemAddCategory;
import com.shop.milk_tea_shop.manager.ItemAddManager;
import com.shop.milk_tea_shop.request.ItemAddsQueryRequest;
import com.shop.milk_tea_shop.service.ItemAddCategoryService;
import com.shop.milk_tea_shop.service.ItemAddService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品附加信息表 前端控制器
 * </p>
 *
 * @author yjw
 * @since 2024-06-16
 */
@RestController
@RequestMapping("/itemAdd")
public class ItemAddController {

    @Autowired
    private ItemAddManager itemAddManager;

    @Autowired
    private ItemAddService itemAddService;

    @Autowired
    private ItemAddCategoryService itemAddCategoryService;

    /**
     * 根据商品id，获取附加信息列表
     * @param itemId    商品id
     * @return
     */
    @GetMapping("/{itemId}")
    public Result<List<ItemAddDTO>> getItemAdds(@PathVariable Long itemId) {
        List<ItemAddDTO> itemAdds = itemAddManager.getItemAdds(itemId);
        return new Result<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS, itemAdds, new GlobalInfo());
    }

    /**
     * 获取附加信息菜单
     * @return
     */
    @PostMapping("/menu")
    public Result<List<ItemAddDTO>> itemAddsMenu(ItemAddsQueryRequest request) {
        List<ItemAddDTO> itemAddDTOS = itemAddManager.itemAddsMenu(request);
        return new Result<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS, itemAddDTOS, new GlobalInfo());
    }

    @PutMapping("/adds")
    public Result<Boolean> updateItemAdd(ItemAdd itemAdd) {
        boolean result = itemAddService.updateById(itemAdd);
        return new Result<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS, result, new GlobalInfo());
    }

    @PutMapping("/category")
    public Result<Boolean> updateAddCategory(ItemAddCategory itemAddCategory) {
        boolean result = itemAddCategoryService.updateById(itemAddCategory);
        return new Result<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS, result, new GlobalInfo());
    }

    @DeleteMapping("/{addId}")
    public Result<Boolean> deleteItemAdd(@PathVariable Long addId) {
        ItemAdd itemAdd = itemAddService.getById(addId);
        itemAdd.setStatus(0);
        return updateItemAdd(itemAdd);
    }

    @DeleteMapping("/{addCategoryId}")
    public Result<Boolean> deleteAddCategory(@PathVariable Long addCategoryId) {
        return itemAddCategoryService.deleteCategoryAndAdds(addCategoryId);
    }

    @PostMapping("/insertCategory")
    public Result<Boolean> insertAddCategory(ItemAddCategory itemAddCategory, ItemAdd itemAdd) {
        return itemAddCategoryService.insertCategoryAndAddInfo(itemAddCategory, itemAdd);
    }

    @PostMapping("/insertAdd")
    public Result<Boolean> insertAddCategory(ItemAdd itemAdd) {
        boolean result = itemAddService.save(itemAdd);
        return new Result<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS, result, new GlobalInfo());
    }
}
